<?php
class sws_recharge_order_model extends CI_Model {

	public function __construct() {
		$this->sws = $this->load->database('sws', true);
		date_default_timezone_set('Asia/Shanghai');
	}
	public function insert_order($insert) {
		$data = array (
			"TRAN_ID" => $insert['tran_id'],
			"ACCOUNT_NO" => $insert['account_no'],
			"ORDER_SN" => $insert['order_sn'],
			"AMOUNT" => $insert['amount'],
			"PAY_STATUS" => $insert['pay_status'],
			"PAY_TYPE" => $insert['pay_type'],
			"PAY_TIME" => $insert['pay_time'],
			"CREATED_BY" => $insert['created_by'],
			"CREATED_TIME" => $insert['created_time']
		);
		$this->sws->insert("SWS_RECHARGE_ORDER", $data);
		return $this->sws->insert_id();
	}

	public function pay_success($order_info, $order_sn) {
		$payment_order = $order_info['payment_order'];
		$pay_time = $order_info['pay_time'];
		$pay_status = $order_info['pay_status'];
		$modified_by = $order_info['modified_by'];
		$modified_time = $order_info['modified_time'];
		$payment_fee = $order_info['payment_fee'];
		$sql = "update SWS_RECHARGE_ORDER
					set PAYMENT_ORDER='$payment_order',
					PAY_TIME='$pay_time',
					PAY_STATUS='$pay_status',
					MODIFIED_BY='$modified_by',
					MODIFIED_TIME='$modified_time',
					PAYMENT_FEE ='$payment_fee' where PAY_STATUS ='0' and  ORDER_SN ='$order_sn'
					";
		log_message('debug',$payment_order);
		log_result($sql);
		$this->sws->query($sql);
	}
	
	public function update_order_payment_order($order_sn,$payment_order){
		$sql = "update SWS_RECHARGE_ORDER set PAYMENT_ORDER='$payment_order' where  ORDER_SN ='$order_sn'";
		$this->sws->query($sql);
	}
	
	public function query_order_by_payment_order($payment_order){
		$sql 	= "select PAY_STATUS from SWS_RECHARGE_ORDER where PAYMENT_ORDER = '$payment_order' limit 1 ";
		$query	= $this->sws->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
		
	}
	public function get_pay_status($order_sn) {
		$sql = "select PAY_STATUS from SWS_RECHARGE_ORDER where ORDER_SN = '$order_sn' ";
		
		$query = $this->sws->query($sql);
		if ($query->num_rows() > 0) {
			$row = $query->row_array();
			return $row['PAY_STATUS'];
		} else {
			return 1;
		}
	}
	public function query_order_by_sn($order_sn) {
		$sql = "select sro.TRAN_ID,sro.ACCOUNT_NO,sro.AMOUNT,tr.MERCHANT_ID,tr.CREATED_TIME from SWS_RECHARGE_ORDER as sro  left join SWS_TRANSACTION as tr on tr.TRAN_ID = sro.TRAN_ID  where sro.ORDER_SN  = '$order_sn' ";
		
		$query = $this->sws->query($sql);
		if ($query->num_rows() > 0) {
			return $query->row();
		}
	}

	public function query_payment_pay_status_by_sn($order_sn) {
		$sql = "select TRAN_ID,PAYMENT_PAY_STATUS from SWS_RECHARGE_ORDER where ORDER_SN = '$order_sn' ";
		$query = $this->sws->query($sql);

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
	}

	public function change_recharge_payment_status($order_sn, $payment_status) {
		$payment_status = $this->sws->escape($payment_status);
		$sql = "update SWS_RECHARGE_ORDER set PAYMENT_PAY_STATUS = $payment_status where ORDER_SN = '$order_sn' ";
		
		$this->sws->query($sql);
	}
	
	public function change_tran_payment_status($tran_id,$payment_status) {
		$tran_id		=$this->sws->escape($tran_id);
		$payment_status	=$this->sws->escape($payment_status);
		$sql ="update SWS_TRANSACTION 
				set PAYMENT_PAY_STATUS=$payment_status where TRAN_ID =$tran_id";
		$this->sws->query($sql);
	}

}
